package com.mjk.manager.magicapi.plugin.webservice.wsdl;

import cn.hutool.core.util.XmlUtil;
import com.mjk.manager.amis.AmisModelInfo;
import com.mjk.manager.magicapi.plugin.webservice.constns.MagicWebServiceConstns;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.CollectionUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * soap 协议返回内容解析 为正常 xml
 */
public class MagicApiSoapUtils {
    /**
     * 解析soap协议 返回return 之内的内容
     * @param soap
     * @return
     */
    public static String parasSoap(String soap){
        Document document = XmlUtil.parseXml(soap);
        Element root = document.getDocumentElement();
        NodeList returnList = root.getElementsByTagName(MagicWebServiceConstns.RETURN_TAG);
        if(returnList==null || returnList.getLength()<=0){
            return "";
        }
        Element returnElement = (Element) returnList.item(0);
        return returnElement.getTextContent();
    }

    public static void main(String[] args) {
        String soap = """
               <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                  <soap:Body>
                    <ns2:testDrgsResponse xmlns:ns2="http://resource.webservice.plugin.magicapi.manager.mjk.com/">
                        <return>&lt;list&gt;
                          &lt;item&gt;
                            &lt;id&gt;2&lt;/id&gt;
                            &lt;title&gt;Hello world&lt;/title&gt;
                            &lt;type&gt;page&lt;/type&gt;
                            &lt;path&gt;2&lt;/path&gt;
                            &lt;info&gt;{&amp;quot;type&amp;quot;:&amp;quot;page&amp;quot;,&amp;quot;title&amp;quot;:&amp;quot;Hello world&amp;quot;,&amp;quot;body&amp;quot;:[{&amp;quot;type&amp;quot;:&amp;quot;tpl&amp;quot;,&amp;quot;tpl&amp;quot;:&amp;quot;初始页面&amp;quot;,&amp;quot;wrapperComponent&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;inline&amp;quot;:false,&amp;quot;id&amp;quot;:&amp;quot;u:b21daa1a5f48&amp;quot;},{&amp;quot;type&amp;quot;:&amp;quot;grid&amp;quot;,&amp;quot;columns&amp;quot;:[{&amp;quot;body&amp;quot;:[],&amp;quot;id&amp;quot;:&amp;quot;u:15d2ec2dc91f&amp;quot;},{&amp;quot;body&amp;quot;:[],&amp;quot;id&amp;quot;:&amp;quot;u:a99635bd38d8&amp;quot;}],&amp;quot;id&amp;quot;:&amp;quot;u:7d451e182c9b&amp;quot;}],&amp;quot;id&amp;quot;:&amp;quot;u:5f6fd77499d3&amp;quot;,&amp;quot;asideResizor&amp;quot;:false,&amp;quot;pullRefresh&amp;quot;:{&amp;quot;disabled&amp;quot;:true}}&lt;/info&gt;
                          &lt;/item&gt;
                        &lt;/list&gt;
                        </return>
                    </ns2:testDrgsResponse>
                  </soap:Body>
               </soap:Envelope>
                """;
        System.out.println(parasSoap(soap));
    }
}
